﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace RuoTeng.Util
{
 /// <summary>
 /// 正在表达式帮助累
 /// </summary>
  public static  class RegularHelper
    {
      /// <summary>
      /// 根据一个规则获取返回一个List
      /// </summary>
      /// <param name="content"></param>
      /// <param name="pattern"></param>
      /// <returns></returns>
      public static List<string>  getRegular(string content,string pattern,string sonPattern,string soncontent)
      {
          MatchCollection matches = Regex.Matches(content, pattern);
          List<string> list = new List<string>();
            
            foreach (Match match in matches)
            {
               
                GroupCollection groups = match.Groups;
                if (groups.Count > 0)
                {
                    string result = getStrFromPattern(sonPattern, soncontent);
                    if (result.Trim() != "" && !list.Contains(result))
                    {
                        list.Add(result);
                    }
                }

            }

            return list;
      }

      /// <summary>
      /// 根据一个规则获取内容，默认规则内容为content
      ///  private string asinpattern = @"(?isx)ASIN%3D(?<asin>.*)";//asin        
      /// </summary>
      /// <param name="pattern"></param>
      /// <param name="content"></param>
      
      /// <returns></returns>
      public static string getStrFromPattern(string pattern, string content)
      {
          Match match = Regex.Match(content, pattern);
          string result = "";
          if (match.Success)
          {
              GroupCollection groups = match.Groups;
              result = groups["content"].ToString();
          }
          return result;
      }


      public static string NoHTML(string Htmlstring)
      {
          //删除脚本
          Htmlstring = Htmlstring.Replace("\r\n", "");
          Htmlstring = Regex.Replace(Htmlstring, @"<script.*?</script>", "", RegexOptions.IgnoreCase);
          Htmlstring = Regex.Replace(Htmlstring, @"<style.*?</style>", "", RegexOptions.IgnoreCase);
          Htmlstring = Regex.Replace(Htmlstring, @"<.*?>", "", RegexOptions.IgnoreCase);
          //删除HTML
          Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
          Htmlstring = Regex.Replace(Htmlstring, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase);
          Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase);
          Htmlstring = Regex.Replace(Htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase);
          Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase);
          Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
          Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
          Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
          Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", "", RegexOptions.IgnoreCase);
          Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase);
          Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);
          Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);
          Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase);
          Htmlstring = Regex.Replace(Htmlstring, @"&#(\d+);", "", RegexOptions.IgnoreCase);
          Htmlstring = Htmlstring.Replace("<", "");
          Htmlstring = Htmlstring.Replace(">", "");
          Htmlstring = Htmlstring.Replace("\r\n", "");

          return Htmlstring;
      }

    }
}
